package com.proj.hot.update;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import com.proj.hot.GlobalParams;
import com.proj.hot.widget.XWDialogLoading;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by user on 17/8/7.
 */
public class AppUpdate {



    public static void getVer(final Context context){

        try {

            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("sn", GlobalParams.SN);




            RequestParams params = new RequestParams(GlobalParams.reqHost + "attestation/getAppUpgrade");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        JSONObject jData=new JSONObject(s);

                        JSONObject respbody=jData.getJSONObject("respbody");
                        JSONObject data=respbody.getJSONObject("data");
                        if(data!=null){

                            int ver=data.getInt("verison");
                            PackageManager manager = context.getPackageManager();
                            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
                            int version = info.versionCode;

                            if (ver>version){
                                Toast.makeText(context,"正在准备版本升级更新...",Toast.LENGTH_SHORT).show();
                                String apkurl=data.getString("path");
                                new XWDialogLoading(context,apkurl).show();


                            }


                        }



                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        }catch (Exception e){
            e.printStackTrace();
        }
    }



    }



